import request from '@/utils/request'
import type { ApiResponse } from '@/types/api'

export interface FileVersionVO {
  id: string
  fileId: string
  versionNumber: number
  storageObjectId: string
  changeLog?: string
  versionTags?: string
  isCurrent: boolean
  createTime?: string
}

export interface CreateVersionRequest {
  fileId: string
  storageObjectId: string
  changeLog?: string
  versionTags?: string
  setAsCurrent?: boolean
}

export interface SwitchVersionRequest {
  fileId: string
  versionId: string
}

export interface RollbackVersionRequest {
  fileId: string
  versionNumber: number
  changeLog?: string
}

export const versionApi = {
  create: (req: CreateVersionRequest): Promise<ApiResponse<FileVersionVO>> =>
    request.post('/file/version/create', req),

  listByFile: (fileId: string): Promise<ApiResponse<FileVersionVO[]>> =>
    request.get(`/file/version/list/${fileId}`),

  current: (fileId: string): Promise<ApiResponse<FileVersionVO>> =>
    request.get(`/file/version/current/${fileId}`),

  byNumber: (fileId: string, versionNumber: number): Promise<ApiResponse<FileVersionVO>> =>
    request.get(`/file/version/${fileId}/${versionNumber}`),

  switchTo: (req: SwitchVersionRequest): Promise<ApiResponse<void>> =>
    request.put('/file/version/switch', req),

  rollback: (req: RollbackVersionRequest): Promise<ApiResponse<FileVersionVO>> =>
    request.post('/file/version/rollback', req),

  delete: (versionId: string, force = false): Promise<ApiResponse<void>> =>
    request.delete(`/file/version/${versionId}`, { params: { force } }),

  deleteAllByFile: (fileId: string): Promise<ApiResponse<void>> =>
    request.delete(`/file/version/all/${fileId}`),

  compare: (fileId: string, fromVersion: number, toVersion: number): Promise<ApiResponse<string>> =>
    request.get(`/file/version/compare/${fileId}`, { params: { fromVersion, toVersion } }),

  countByFile: (fileId: string): Promise<ApiResponse<number>> =>
    request.get(`/file/version/count/${fileId}`),

  batchCount: (fileIds: string[]): Promise<ApiResponse<Record<string, number>>> =>
    request.post('/file/version/batch-count', fileIds),

  cleanup: (fileId: string, keepCount: number): Promise<ApiResponse<number>> =>
    request.post(`/file/version/cleanup/${fileId}`, null, { params: { keepCount } })
}

export default versionApi


